<template>
    <view style="z-index: 99999;" v-if="show" class="mask" @click="toggleMask" @touchmove.stop.prevent="stopPrevent"
        :style="{backgroundColor: backgroundColor}">
        <view class="mask-content" @click.stop.prevent="stopPrevent" style="height: 100%!important;" :style="[{
				height: config.height, 
				transform: transform
			}]">
            <!-- <u-navbar back-text="返回" title="剑未配妥，出门已是江湖"></u-navbar> -->
            <view class="publish-wrap bg-white">
                <view class="cu-bar bg-blue"
                    style="box-shadow:0 0.5px 3px rgba(0, 0, 0, 0.1);min-height: 45px!important;">
                    <view class="action" style="width: 70px;justify-content: flex-start;" @click="toggleMask">
                        <text class="cuIcon-close"></text>
                    </view>
                    <view class="content">
                        动态发布
                    </view>
                </view>
                <view class="input-box padding-sm">
                    <u-input type="textarea" :height="height" :auto-height="autoHeight" :border="border"
                        placeholder="请输入..." v-model="content" />
                    <!-- <te  xtarea class="area-top" placeholder="碎碎念..." v-model="content"></textarea> -->
                </view>
                <view class="cu-form-group img-box">
                    <!-- <view class="next-title">上传</view> -->
                    <view class="img-wrap margin-none">
                        <easy-upload @delImage="dellistimg" @typesel="seltype" :dataList="imageList"
                            :uploadUrl="$cfg.uploadurl" :types="category" deleteUrl='http://localhost:3000/upload'
                            :uploadCount="6" @successImage="successImage" @successVideo="successvideo"></easy-upload>

                    </view>
                </view>
                <view class="u-m-t-20">
                    <u-cell-group>
                        <u-cell-item icon="map" @click="f_map" :icon-style="aaa" :title="maptitle"
                            :class="maptitle!='位置'?'aa':''"></u-cell-item>
                    </u-cell-group>
                </view>
                <view class="bottom-btn flex-center" @click="f_release">
                    <view class="btn">发布</view>
                </view>
                <u-toast ref="uToast" />
            </view>
        </view>
    </view>
</template>

<script>
    import { mapMutations, mapState } from 'vuex';
    import {
        isnull
    } from '@/common/lib/fun.js'
    export default {
        data() {
            return {
                CustomBar: this.CustomBar,
                aaa: {
                    color: '#001f3f',
                },
                maptitle: '位置',
                imageList: [],
                category: 'image',
                height: 200,
                autoHeight: true,
                border: true,
                imgList: [],
                content: '',
                lat: '',
                lng: '',
                transform: 'translateY(50vh)',
                timer: 0,
                backgroundColor: 'rgba(0,0,0,0)',
                show: false,
                config: {},
            };
        },
        props: {
            contentHeight: {
                type: Number,
                default: 0
            },
            //是否是tabbar页面
            hasTabbar: {
                type: Boolean,
                default: false
            },
            shareList: {
                type: Array,
                default: function () {
                    return [];
                }
            }
        },
        created() {
            const appheight = uni.getSystemInfoSync();
            const height = appheight.windowHeight + 'px';
            console.log(appheight);
            this.config = {
                height: height,
                transform: `translateY(${height})`,
                backgroundColor: 'rgba(0,0,0,.4)',
            }
            this.transform = this.config.transform;
        },
        methods: {
            toggleMask() {
                //防止高频点击
                if (this.timer == 1) {
                    return;
                }
                this.timer = 1;
                setTimeout(() => {
                    this.timer = 0;
                }, 500)

                if (this.show) {
                    this.transform = this.config.transform;
                    this.backgroundColor = 'rgba(0,0,0,0)';
                    setTimeout(() => {
                        this.show = false;
                        this.hasTabbar && uni.showTabBar();
                    }, 200)
                    return;
                }

                this.show = true;
                //等待mask重绘完成执行
                if (this.hasTabbar) {
                    uni.hideTabBar({
                        success: () => {
                            setTimeout(() => {
                                this.backgroundColor = this.config.backgroundColor;
                                this.transform = 'translateY(0px)';
                            }, 10)
                        }
                    });
                } else {
                    setTimeout(() => {
                        this.backgroundColor = this.config.backgroundColor;
                        this.transform = 'translateY(0px)';
                    }, 10)
                }
            },
            //防止冒泡和滚动穿透
            stopPrevent() { },
            //分享操作
            shareToFriend(type) {
                this.$api.msg(`分享给${type}`);
                this.toggleMask();
            },
            ...mapMutations(['setsfrelease']),
            dellistimg(i) {
                this.imageList.splice(i, 1)
            },
            f_release() {
                var t = this
                var obj = {
                    title: t.content,
                    type: t.category,
                    attachment: JSON.stringify(t.imageList),
                    userDept: t.$cfg.user().deptId,
                    userId: t.$cfg.user().id,
                    userName: t.$cfg.user().name,
                    userId: t.$cfg.user().id,
                    userNum: t.$cfg.user().userNum,
                    address: t.maptitle,
                    lng: t.lng,
                    lat: t.lat,
                    userUrl: t.$cfg.user().userUrl,
                }
                if (t.maptitle == '我的位置' || t.maptitle == '位置') {
                    obj.address = ''
                }
                t.$api.req_post('staff_grow/staff/stDynamic/add', obj, 'json').then(res => {
                    if (res.success) {
                        this.$refs.uToast.show({
                            title: '发布成功',
                            type: 'success',
                            // url: '/pages/home/home',
                        })
                        this.toggleMask();
                        this.content = '';
                        this.$emit('loadlist');
                        t.setsfrelease(true)
                    } else {
                        this.$refs.uToast.show({
                            title: '异常错误',
                            type: 'error'
                        })
                    }
                })
            },
            f_map() {
                var self = this
                uni.chooseLocation({
                    success: function (res) {
                        self.maptitle = res.name
                        self.lat = res.latitude
                        self.lng = res.longitude
                        console.log('位置名称：' + res.name);
                        console.log('详细地址：' + res.address);
                        console.log('纬度：' + res.latitude);
                        console.log('经度：' + res.longitude);
                    }
                });
            },
            seltype(a) {
                this.category = a
            },
            successImage(e) {
                var t = this
                // #ifdef MP-WEIXIN
                t.imageList.push(e)
                // #endif
                // uni.showModal({ 
                //     content: '上传成功,详细信息请看控制台'
                // })
            },
            successvideo(e) {
                var t = this
                // #ifdef MP-WEIXIN
                t.imageList = []
                t.imageList.push(e)
                // #endif
                // t.imageList.push(e)
                console.log(t.imageList);
            },
            // 选择图片
            ChooseImage() {
                let that = this
                uni.chooseImage({
                    count: 9, //默认9
                    sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
                    sourceType: ['album'], //从相册选择
                    success: (res) => {
                        if (that.imgList.length != 0) {
                            that.imgList = that.imgList.concat(res.tempFilePaths)
                        } else {
                            that.imgList = res.tempFilePaths
                        }
                    }
                });
            },
            //显示删除弹窗
            DelImg(e) {
                let that = this
                that.imgList.splice(e.currentTarget.dataset.index, 1)
            },
            // 预览图片
            ViewImage(index) {
                let that = this
                uni.previewImage({
                    current: index,
                    urls: that.imgList
                })
            },
        }
    }
</script>

<style lang='scss'>
    .publish-wrap {
        .input-box {
            .area-top {
                width: 100%;
                box-shadow: inset 0 -1px 0 0 #ECECEC;
                min-height: 280rpx;
            }
        }

        .img-box {
            padding: 30rpx;
            display: flex;
            flex-direction: column;
            margin-top: 20rpx;
            margin-bottom: 160rpx;

            .next-title {
                width: 100%;
                font-size: 36rpx;
                color: var(--mainColor);
            }

            .font-red {
                color: #ff0000;
            }

            .img-wrap {
                width: 100%;
                font-size: 28rpx;
                line-height: 44rpx;
                color: #999999;
                margin: 20rpx 0;
            }

            .add-img {
                border: 4rpx dashed #999;
                font-size: 40rpx;
            }
        }

        .bottom-btn {
            position: fixed;
            bottom: 0;
            width: 100%;
            padding: 20rpx 0;

            .btn {
                border-radius: 40rpx;
                display: flex;
                align-items: center;
                justify-content: center;
                color: #FFFFFF;
                border: 1rpx solid var(--activeColor);
                background-color: var(--activeColor);
                box-shadow: 0 1px 10px 0px var(--activeColor);
                width: 400rpx;
                height: 80rpx;
            }
        }
    }

    .bg-img {
        overflow: visible !important;

        .img-item {
            border-radius: 20rpx;
        }

        .close-img {
            position: absolute;
            width: 36rpx !important;
            top: -18rpx;
            right: -18rpx;
        }
    }

    .aa {
        /* color: var(--activeColor); */
    }

    .mask {
        position: fixed;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        display: flex;
        justify-content: center;
        align-items: flex-end;
        z-index: 998;
        transition: .3s;

        .bottom {
            position: absolute;
            left: 0;
            bottom: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100%;
            height: 90rpx;
            background: #fff;
            z-index: 9;
            font-size: $uni-font-size-base + 2rpx;
            color: $font-color-dark;
        }
    }

    .mask-content {
        width: 100%;
        height: 100% !important;
        transition: .3s;
        background: #fff;

        &.has-bottom {
            padding-bottom: 90rpx;
        }

        .view-content {
            height: 100%;
        }
    }

    .share-header {
        height: 110rpx;
        font-size: $uni-font-size-base+2rpx;
        color: font-color-dark;
        display: flex;
        align-items: center;
        justify-content: center;
        padding-top: 10rpx;

        &:before,
        &:after {
            content: '';
            width: 240rpx;
            heighg: 0;
            border-top: 1px solid $border-color-base;
            transform: scaleY(.5);
            margin-right: 30rpx;
        }

        &:after {
            margin-left: 30rpx;
            margin-right: 0;
        }
    }

    .share-list {
        display: flex;
        flex-wrap: wrap;
    }

    .share-item {
        min-width: 33.33%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        height: 180rpx;

        image {
            width: 80rpx;
            height: 80rpx;
            margin-bottom: 16rpx;
        }

        text {
            font-size: $uni-font-size-base;
            color: $uni-text-primary;
        }
    }
</style>